package dq.study.tomcatresearch;

import org.apache.coyote.Adapter;
import org.apache.coyote.Request;
import org.apache.coyote.Response;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.net.SocketEvent;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/11/8 14:55
 */
public class MyHandler implements Adapter {

    public void service(Request request, Response response) throws Exception {
        // 请求处理
        System.out.println("Hi, Boss. I am handling the reuqest!");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(baos));
        writer.println("Not Hello World");
        writer.flush();

        ByteChunk byteChunk = new ByteChunk();
        byteChunk.append(baos.toByteArray(), 0, baos.size());
        response.doWrite(byteChunk);
    }

    public boolean prepare(Request req, Response res) throws Exception {
        return false;
    }

    public boolean asyncDispatch(Request req, Response res, SocketEvent status) throws Exception {
        return false;
    }

    public void log(Request req, Response res, long time) {

    }

    public void checkRecycled(Request req, Response res) {

    }

    public String getDomain() {
        return null;
    }


}
